#!/usr/bin/env ruby

require 'rubygems'
require 'postgres'
puts "main__"

calls = [

           ["UK London", "+4455665566", "Holland Amsterdam",
"+3155665566", "17"],

           ["UK London", "+4455665566", "Sweden Stockholm",
"+3155665566", "7"],

           ["UK London", "+4455665566", "France Paris", "+3355665566",
"10"],

           ["UK London", "+4455665566", "Canada Toronto",
"+3355665566", "15"],

           ["UK London", "+4455665566", "Poland Krakow", "+4855665566",
"9"],

           ["UK London", "+4455665566", "Ireland Dublin",
"+3555665566", "27"],

           ["UK London", "+4455665566", "India Dehli", "+4955665566",
"20"],

           ["UK London", "+4455665566", "Argentina BuenosAires",
"+3255665566", "23"],

           ["UK London", "+4455665566", "Belgium Antwerp",
"+3255665566", "10"],

           ["UK London", "+4455665566", "Australia Sydney",
"+3255665566", "9"],

           ["UK London", "+4455665566", "Germany Berlin",
"+4955665566", "7"],

           ["UK London", "+4455665566", "USA NewYork", "+3255665566",
"16"],

           ["UK London", "+4455665566", "SouthAfrica Johannesburg",
"+4955665566", "14"]

     ]

def connect_db
  begin
      conn = PGconn.new("localhost", 5432, nil, nil ,
"voicedata_development", "nauman", "password")
  rescue PGError => err
      p "Exception:", err
  end

  return conn if conn
end

conn = connect_db

for call in calls
   source_country = call[0].split()[0]
   source_region = call[0].split()[1]
   destination_country = call[2].split()[0]
   destination_region = call[2].split()[1]
   source_number = call[1]
   destination_number = call[3];
   call_duration = call[4]

   sql =<<-SQL
   insert into voice_calls
   (source_country, destination_country, source_region,
destination_region, source_number, destination_number, call_duration)
   VALUES
   ('#{source_country}', '#{destination_country}', '#{source_region}',
'#{destination_region}', '#{source_number}',
   '#{destination_number}', '#{call_duration}')
   SQL
   puts sql

   conn.exec(sql)
end

conn.close